﻿using System.Collections.Specialized;
using System.Text;

namespace System.Web
{

    /// <summary>
    /// Replicated from System.Web to avoid having to target entire 4.0 framework and only the client profile instead.
    /// </summary>
    internal sealed class HttpUtility
    {

        /// <summary>
        /// Parses the query string.
        /// </summary>
        /// <param name="query">The query.</param>
        /// <returns></returns>
        public static NameValueCollection ParseQueryString(string query)
        {
            return ParseQueryString(query, Encoding.UTF8);
        }

        /// <summary>
        /// Parses the query string.
        /// </summary>
        /// <param name="query">The query.</param>
        /// <param name="encoding">The encoding.</param>
        /// <returns></returns>
        public static NameValueCollection ParseQueryString(string query, Encoding encoding)
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }
            if (encoding == null)
            {
                throw new ArgumentNullException("encoding");
            }
            if ((query.Length > 0) && (query[0] == '?'))
            {
                query = query.Substring(1);
            }
            return new HttpValueCollection(query, false, true, encoding);
        }

        /// <summary>
        /// URLs the encode unicode.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        internal static string UrlEncodeUnicode(string value)
        {
            return UrlEncodeUnicode(value, false);
        }

        /// <summary>
        /// URLs the encode unicode.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="ignoreAscii">The ignore ASCII.</param>
        /// <returns></returns>
        internal static string UrlEncodeUnicode(string value, bool ignoreAscii)
        {
            if (value == null)
            {
                return null;
            }
            int length = value.Length;
            StringBuilder builder = new StringBuilder(length);
            for (int i = 0; i < length; i++)
            {
                char ch = value[i];
                if ((ch & 0xff80) == 0)
                {
                    if (ignoreAscii || HttpEncoderUtility.IsUrlSafeChar(ch))
                    {
                        builder.Append(ch);
                    }
                    else if (ch == ' ')
                    {
                        builder.Append('+');
                    }
                    else
                    {
                        builder.Append('%');
                        builder.Append(HttpEncoderUtility.IntToHex((ch >> 4) & '\x000f'));
                        builder.Append(HttpEncoderUtility.IntToHex(ch & '\x000f'));
                    }
                }
                else
                {
                    builder.Append("%u");
                    builder.Append(HttpEncoderUtility.IntToHex((ch >> 12) & '\x000f'));
                    builder.Append(HttpEncoderUtility.IntToHex((ch >> 8) & '\x000f'));
                    builder.Append(HttpEncoderUtility.IntToHex((ch >> 4) & '\x000f'));
                    builder.Append(HttpEncoderUtility.IntToHex(ch & '\x000f'));
                }
            }
            return builder.ToString();
        }

        /// <summary>
        /// URLs the decode.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="encoding">The encoding.</param>
        /// <returns></returns>
        internal static string UrlDecode(string value, Encoding encoding)
        {
            return HttpEncoderUtility.UrlDecode(value, encoding);
        }
    }
}
